package com.zat.mes.andrmes.dofun.qualitytest.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.QcPlanBean
import com.zat.mes.andrmes.utils.SpannableStrUtil
import kotlinx.android.synthetic.main.item_item_quality_plan.view.*

/**
 * 质检计划
 */
class QualityPlanAdapter(layoutResId: Int) :
    BaseQuickAdapter<QcPlanBean, BaseViewHolder>(layoutResId) {


    override fun convert(helper: BaseViewHolder, item: QcPlanBean) {

        helper.setText(R.id.tvNameQualityPlanItem, item?.productName)
        var styleNo = context.getString(R.string.text_style) + " " + item?.styleNo
        var color = context.getString(R.string.text_color) + " " + item?.color
        var size = context.getString(R.string.text_size) + " " + item?.size
        val textColor = ContextCompat.getColor(context, R.color.color_text_grey)
        helper.setText(
            R.id.tvStyleNoQualityPlanItem,
            SpannableStrUtil.getColorSpannable(styleNo, 3, styleNo.length, textColor)
        )
        helper.setText(
            R.id.tvColorQualityPlanItem,
            SpannableStrUtil.getColorSpannable(color, 3, color.length, textColor)
        )
        helper.setText(
            R.id.tvSizeQualityPlanItem,
            SpannableStrUtil.getColorSpannable(size, 3, size.length, textColor)
        )

        val ivHideShow = helper.getView<ImageView>(R.id.ivQualityPlanShowHide)
        val llPro = helper.getView<LinearLayout>(R.id.llProcQualityPlanItem)
        if (item?.workQcPlanList !== null) {
            llPro.removeAllViews()
            for (orderCraftprocess in item?.workQcPlanList) {
                val inflate = LayoutInflater.from(context)
                    .inflate(R.layout.item_item_quality_plan, null)
                inflate.btnMoreQualityPlanItem.visibility = View.GONE
                inflate.tvProcNameQualityPlanItem.text = orderCraftprocess.procName
                inflate.tvEmpNameQualityPlanItem.text = orderCraftprocess.planEmpName
                inflate.tvDateQualityPlanItem.text = orderCraftprocess.qcDate
                if ("3".equals(orderCraftprocess.status)) {
                    //未完成
                    inflate.tvTypeQualityPlanItem.text = context.getString(R.string.quality_no)
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_not_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_not_finished
                        )
                    )
                } else if ("5".equals(orderCraftprocess.status)) {
                    inflate.tvTypeQualityPlanItem.text = context.getString(R.string.quality_has)
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_has_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_has_finished
                        )
                    )
                } else {
                    inflate.tvTypeQualityPlanItem.text = orderCraftprocess.statusname
                    inflate.tvTypeQualityPlanItem.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_not_finished
                        )
                    )
                    inflate.tvTypeQualityPlanItem.setBackgroundColor(
                        ContextCompat.getColor(
                            context,
                            R.color.text_bg_not_finished
                        )
                    )
                }
                llPro.addView(inflate)
            }
        }

        if (item?.show!!) {
            ivHideShow.setImageResource(R.mipmap.icon_shengchanjihua_heqi)
            llPro.visibility = View.VISIBLE
        } else {
            ivHideShow.setImageResource(R.mipmap.icon_shengchanjihua_zhankai)
            llPro.visibility = View.GONE
        }
        helper.getView<LinearLayout>(R.id.llBtnQualityPlanShowHide).setOnClickListener {
            //展开收起
            if (item?.show!!) {
                item?.show = false
            } else {
                item?.show = true
            }
            notifyDataSetChanged()
        }
    }
}